//
//  ViewController.m
//  20501
//
//  Created by student on 16/3/22.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

#import <pthread.h>
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
void * run(void *param)
{
    for(int i= 0;i< 10000;i++)
    {
        NSLog(@"子线程1: %i",i);
    }
    return NULL;
}

void * run2(void *param)
{
    for(int i= 0;i< 10000;i++)
    {
        NSLog(@"子线程2: %i",i);
    }
    return NULL;
}

- (IBAction)pressed:(id)sender
{
    pthread_t thread;
    pthread_create(&thread, NULL, run, NULL); // 创建子线程
    
    pthread_t thread2;
    pthread_create(&thread2, NULL, run2, NULL);
    
    NSThread *thred3=[[NSThread alloc]initWithTarget:self selector:@selector(multithread:) object:@"子线程3"];
    [thred3 start];
    [NSThread detachNewThreadSelector:@selector(multithread:) toTarget:self withObject:@"子线程4"];
    
//    for(int i= 0;i< 10000;i++)
//    {
//        NSLog(@"主线程: %i",i);
//    }
    dispatch_queue_t queue2=dispatch_queue_create("子线程5", NULL);
    dispatch_async(queue2, ^{
        for(int i=1;i<10000;i++)
        {
            NSLog(@"子线程5,%i",i);
        }
    });
    
    NSOperationQueue *threadqueue2=[[NSOperationQueue alloc]init];
    threadqueue2.maxConcurrentOperationCount=2;
    NSInvocationOperation *op1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(multithread:) object:@"子线程6"];
     NSInvocationOperation *op2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(multithread:) object:@"子线程7"];
     NSInvocationOperation *op3=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(multithread:) object:@"子线程8"];
    [threadqueue2 addOperation:op1];
    [threadqueue2 addOperation:op2];
    [threadqueue2 addOperation:op3];
    [self performSelectorInBackground:@selector(multithread:) withObject:@"后台线程"];
 
}
-(void)multithread:(NSString *)threadname
{
    for(int i=0;i<10000;i++)
    {
        NSLog(@"%@:%i",threadname,i);
    }
}
- (IBAction)changed:(id)sender
{
     NSLog(@"%s",__func__);
}

@end
